package Reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 使用指定构造器实例化对象
 * constructor.newInstance(传参)
 */
public class ReflectDemo3 {
    public static void main(String[] args) {

        System.out.println(new Person("刘桑",55));

        try {
            Class cls = Class.forName("Reflect.Person");
            // 获取指定的构造器
            Constructor constructor = cls.getConstructor(String.class, int.class);
            Person cao = (Person) constructor.newInstance("苍桑", 55);

            System.out.println(cao);


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
